package com.manlu.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manlu.mapper.UserMapper;
import com.manlu.pojo.User;
import com.manlu.vo.BaseResult;
import com.manlu.vo.QueryInfo;
import com.manlu.vo.UserPageInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther 漫路h
 * Created by 2020-05-04 20:48
 */
@Transactional
@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 查询小程序用户列表,(分页)
     * @param queryInfo
     * @return
     */
    public BaseResult selectAllByPage(UserPageInfoVo queryInfo) {
        //开启分页
        PageHelper.startPage(queryInfo.getPageNum(),queryInfo.getPageSize());
        //查询所有,根据状态
        List<User> userList = userMapper.selectAllByState(queryInfo.getState());
        //封装
        PageInfo<User> pi = new PageInfo<>(userList);
        //返回
        return BaseResult.ok("ok",pi);
    }

    /**
     * 根据用户id删除用户
     * @param uid 用户uid
     * @return
     */
    public BaseResult deleteUserByUid(Integer uid) {
        //使用通用mapper,根据主键删除
        int i = userMapper.deleteByPrimaryKey(uid);
        //删除成功
        if (i == 1) {
            return BaseResult.ok("ok");
        }
        //删除失败
        return BaseResult.error("error");
    }

    /**
     * 修改小程序用户状态
     * @param state
     * @return
     */
    public BaseResult editUserState(Integer uid,Integer state) {
        int i = userMapper.editUserState(uid,state);
        if (i == 1) {
            return BaseResult.ok("ok");
        }
        return BaseResult.error("err");
    }
}
